package com.ruisoft.business.purchase.controller;

import com.ruisoft.base.controller.IBaseController;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ruisoft.base.controller.BaseController;
import com.ruisoft.business.purchase.service.PurGroupService;

/**
 * Created with IntelliJ IDEA.
 * User: LFC
 * Date: 2015/3/3
 * Time: 19:32
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/purchaseGroup")
public class PurchaseController extends BaseController implements IBaseController {

	@Autowired
	public PurGroupService purGroupService;

    /**
	 * 新增处理
	 * @return
	 */
    @RequestMapping(value = "create", method = RequestMethod.GET)
	public String create() {
        //
		return "redirect:/html/purchasegroup/purchase_group_add.html";
	}

    @Override
    public String edit(String id) {
        return null;
    }

    /**
     * 分页查询采购组主表信息
     * @return
     */
    @Override
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list()  {
        try {
            JSONObject json = getReqData();
            String  result = purGroupService.searchPageGroup(json.toString());
            System.out.println(result);
            response(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/html/purchasegroup/purchase_group_list.html";
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    public String edit() {
        try {
            System.out.println("采购组保存，，，");
            JSONObject json = getReqData();
            System.out.println(json.toString());
            purGroupService.save(json.toString());
            System.out.println(json.toString());
            response(json.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/html/purchasegroup/purchase_group_add.html";
    }


     @RequestMapping(value = "view/{id}", method = RequestMethod.GET)
	public void view(@PathVariable("id") String id) {
    	 String result;
		try {
			//
			result = purGroupService.searchGroup(id);
			//
			response(result);
			
		} catch (Exception e) { 
			//
			e.printStackTrace();
		}

	}

    @Override
    public String update(String id) {
        return null;
    }

    @Override
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id) {
        String result;
        try {
            //
            purGroupService.deletePurGroup(id);
        } catch (Exception e) {
            //
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/savePurchaseGroup")
    public void savePerson() throws Exception {
        System.out.println("采购组保存，，，");
        JSONObject json = getReqData();
        System.out.println(json.toString());
		purGroupService.save(json.toString());
//        String result = purGroupService.searchGroup("22");
        System.out.println(json.toString());
        response(json.toString());
    }

//    @RequestMapping(value = "/savePurchaseGroup/postMethod/{id}", method = RequestMethod.POST)
//    public @ResponseBody String postPerson(@RequestBody String jsonData, @PathVariable String id) throws Exception {
//        String returnValue = "{id:\""+id+"\",name:\"name\"}";
//        JSONObject data = new JSONObject();
//        data.put("id", id);
//        System.out.println(data.toString());
//        System.out.println(jsonData);
//        data.put("jsonData", jsonData);
//        return  data.toString();
//    }

}
